package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.dao.IWithdrawApplyforDao;
import com.hdoit.ares.logical.acc.model.WithdrawApplyfor;
import com.hdoit.ares.logical.acc.model.WithdrawRecord;
import com.hdoit.ares.logical.viewmodel.acc.WithdrawApplyforSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

/**
 * @author caochengmeng
 * @email
 * @createtime 2017年11月20日 16:31:51
 * @comment
 */
@Component
public class WithdrawApplyforDaoImpl extends SuperDao<WithdrawApplyfor> implements IWithdrawApplyforDao {

	@Override
	public List<WithdrawApplyfor> queryList(WithdrawApplyforSearch withdrawApplyforSearch, PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT withdrawApplyfor.*", "withdrawRecord.modifyUserName as trackName",
						"withdrawRecord.modifyDate as trackDate", "withdrawRecord.state as trackState")
						.from(WithdrawApplyfor.class, "withdrawApplyfor")
						.leftJoin(WithdrawRecord.class, "withdrawRecord")
						.on("withdrawApplyfor.aid", "withdrawRecord.id")
						.andWhere("withdrawApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.setMainTableAlias("withdrawApplyfor")
						.addOrder("withdrawApplyfor.createDate", OrderEnum.desc);

		appendFinder(withdrawApplyforSearch, finderExpression);

		if (pagerModel == null) {
			return queryList(finderExpression, withdrawApplyforSearch);
		}
		
		return queryList(finderExpression, withdrawApplyforSearch, pagerModel);
	}

	@Override
	public List<WithdrawApplyfor> queryList(WithdrawApplyforSearch withdrawApplyforSearch) {
		return queryList(withdrawApplyforSearch, null);
	}

	@Override
	public WithdrawApplyfor queryModel(String key){
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("withdrawApplyfor.*", "withdrawRecord.modifyUserName as trackName",
						"withdrawRecord.modifyDate as trackDate", "withdrawRecord.state as trackState")
						.from(WithdrawApplyfor.class, "withdrawApplyfor")
						.leftJoin(WithdrawRecord.class, "withdrawRecord")
						.on("withdrawApplyfor.aid", "withdrawRecord.id")
						.andWhere("withdrawApplyfor.id=#{withdrawId}")
						.setParam("withdrawId", key)
						.andWhere("withdrawApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no);

		return super.queryModel(finderExpression);
	}

	@Override
	public int queryCount(WithdrawApplyforSearch withdrawApplyforSearch) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT withdrawApplyfor.id) as count")
						.from(WithdrawApplyfor.class, "withdrawApplyfor")
						.leftJoin(WithdrawRecord.class, "withdrawRecord")
						.on("withdrawApplyfor.aid", "withdrawRecord.id")
						.andWhere("withdrawApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.setMainTableAlias("withdrawApplyfor");

		appendFinder(withdrawApplyforSearch, finderExpression);
		
		return queryCount(finderExpression, withdrawApplyforSearch);
	}

	private void appendFinder(WithdrawApplyforSearch withdrawApplyforSearch, IFinderExpression finder) {
		if (!StringUtils.isEmpty(withdrawApplyforSearch.getApplyforName())) {
			finder.andWhere("withdrawApplyfor.createUserName like #{aname}")
			.setParam("aname", "%" + withdrawApplyforSearch.getApplyforName() + "%");
		}

		if (!StringUtils.isEmpty(withdrawApplyforSearch.getTrackName())) {
			finder.andWhere("withdrawRecord.modifyUserName like #{hname}")
			.setParam("hname", "%" + withdrawApplyforSearch.getTrackName() + "%");
		}
/*
		if (withdrawApplyforSearch.getStartDate() != null) {
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},withdrawApplyfor.createDate)>=0")
			.setParam("startDate", withdrawApplyforSearch.getStartDate());
		}
		if (withdrawApplyforSearch.getEndDate() != null) {
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},withdrawApplyfor.createDate)<=0")
			.setParam("endDate", withdrawApplyforSearch.getEndDate());
		}*/

		if (!StringUtils.isEmpty(withdrawApplyforSearch.getCheckType())) {
			finder.andWhere("withdrawApplyfor.state=#{astate}")
			.setParam("astate", withdrawApplyforSearch.getCheckType());
		}
		if (!StringUtils.isEmpty(withdrawApplyforSearch.getTrackType())) {
			finder.andWhere("withdrawRecord.state=#{hstate}")
			.setParam("hstate", withdrawApplyforSearch.getTrackType());
		}
	}

	@Override
	public List<WithdrawApplyfor> queryUnderwayWithdraw(String customerId, String state) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("withdrawApplyfor.id", "withdrawApplyfor.state", 
						"withdrawApplyfor.orderNum", "withdrawApplyfor.createDate")
						.from(WithdrawApplyfor.class, "withdrawApplyfor")
						.andWhere("withdrawApplyfor.customerId=#{customerId}")
						.setParam("customerId", customerId)
						.andWhere("withdrawApplyfor.state!=#{state}")
						.setParam("state", state)
						.andWhere("withdrawApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("withdrawApplyfor.createDate", OrderEnum.desc);
		
		return super.queryList(finderExpression);
	}

	@Override
	public List<WithdrawApplyfor> queryFinishedWithdraw(String customerId, String state) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("withdrawApplyfor.id", "withdrawApplyfor.state",
						"withdrawApplyfor.orderNum", "withdrawApplyfor.createDate")
						.from(WithdrawApplyfor.class, "withdrawApplyfor")
						.andWhere("withdrawApplyfor.customerId=#{customerId}")
						.setParam("customerId", customerId)
						.andWhere("withdrawApplyfor.state=#{state}")
						.setParam("state", state)
						.andWhere("withdrawApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("withdrawApplyfor.createDate", OrderEnum.desc);
		return super.queryList(finderExpression);
	}
}
